<template>
  <div>
    <van-popup v-model="show" position="bottom" style="background: none">
      <div class="popup-box">
        <div class="title_box">
          <div class="title">{{ $t("lucky.doc8") }}</div>
          <van-icon name="cross" @click="show = false" class="cross" />
        </div>
        <div class="content">
          <div class="title-box">
            <div class="title">{{ $t("lucky.doc16") }}</div>
            <div class="title">{{ $t("lucky.doc17") }}</div>
            <div class="title">{{ $t("lucky.doc18") }}</div>
          </div>
          <div class="record-list-box">
            <EmptyData v-show="emptyShow" :text="$t('load.no_record')" />
            <van-pull-refresh
              v-model="refreshing"
              :success-text="$t('load.refreshed')"
              @refresh="refreshData"
              head-height="50"
              v-if="!emptyShow"
            >
              <van-list
                v-model="loading"
                :finished="finished"
                :loading-text="$t('load.loading')"
                @load="loadData"
                :immediate-check="true"
              >
                <div class="record-list">
                  <div
                    class="record"
                    v-for="(item, index) in recordList"
                    :key="index"
                  >
                    <div class="amount">{{ item.remark }}</div>
                    <div class="time">{{ item.createTime }}</div>
                    <div class="status">
                      <template v-if="item.status === 1">
                        {{ $t("lucky.doc19") }}
                      </template>
                      <template
                        v-else-if="
                          item.status === 0 && item.rewardType == 'THING'
                        "
                      >
                        <span @click="writeInfo">{{ $t("page.doc99") }}</span>
                      </template>

                      <template v-else>{{ $t("lucky.doc21") }}</template>
                    </div>
                  </div>
                </div>
              </van-list>
            </van-pull-refresh>
          </div>
        </div>
      </div>
    </van-popup>

    <LuckyWriteInfoPop
      v-model="showWriteInfoPop"
      :receiveSn="receiveSn"
      @receiveSuccess="onTab"
    />
  </div>
</template>

<script>
import EmptyData from "@/components/public/EmptyData.vue";
import refreshList from "@/mixins/refreshList.js";
import LuckyWriteInfoPop from "@/views/lucky/components/LuckyWriteInfoPop.vue";

export default {
  components: { LuckyWriteInfoPop, EmptyData },
  mixins: [refreshList],
  props: {
    value: Boolean,
  },
  data() {
    return {
      ruleInfo: {},
      emptyShow: false,
      recordList: [],
      showWriteInfoPop: false,
      receiveSn: "",
    };
  },
  activated() {
    // this.getRuleInfo();
  },
  computed: {
    show: {
      get() {
        return this.value;
      },
      set(value) {
        this.$emit("input", value);
      },
    },
  },
  watch: {
    show(show) {
      if (show && (this.emptyShow || this.recordList.length > 0)) {
        this.onTab();
        // this.getRuleInfo()
      }
    },
  },
  methods: {
    jump() {
      this.$router.push("/welfare");
      this.show = false;
    },
    writeInfo() {
      this.$router.push("/customer");
      // this.showWriteInfoPop = true;
      // this.receiveSn = item.sn
    },
    async getRecordList() {
      let url = this.URL.lucky.record;
      let { page, pageSize } = this;
      let params = { page, pageSize, type: 1 };

      let { data, code, message } = await this.$post(url, params).catch(
        (err) => {
          this.$toast(err.message);
          this.emptyShow = this.recordList.length == 0;
        }
      );
      if (code == 1005) {
        setTimeout(() => this.getRecordList(), 500);
        return;
      }
      if (code != 0) return this.$toast(message);
      this.recordList.push(...data.list);
      this.emptyShow = this.recordList.length == 0;
      this.judgement(data.list.length, data.page.totalPage);
    },
    /**
     * 继续加载数据列表
     */
    loadData() {
      this.getRecordList();
    },
    /**
     * 重新加载页面
     */
    refreshData() {
      this.refreshInit();
      this.recordList = [];
      this.getRecordList();
    },
    onTab() {
      this.initData();
      this.recordList = [];
      this.getRecordList();
    },
  },
};
</script>

<style lang="less" scoped>
.popup-box {
  line-height: 1.3;
  background: #1a202e;
  padding: 0.32rem 0.32rem;
  border-radius: 0.4rem 0.4rem 0 0;

  .title_box {
    padding: 0 0.32rem;
    position: relative;

    .title {
      padding: 0 0.32rem;
      text-align: center;
      color: #fff;
      font-family: "PingFang SC";
      font-size: 0.36rem;
      font-weight: 600;
    }

    .cross {
      font-size: 0.32rem;
      color: #fff;
      font-weight: bold;
      cursor: pointer;
      position: absolute;
      right: 0;
      top: 50%;
      transform: translate(0, -50%);
    }
  }

  .content {
    margin-top: 0.4rem;

    .title-box {
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 0.28rem 0.2rem;

      .title {
        flex: 1;
        color: rgba(255, 255, 255, 0.6);
        font-family: "PingFang SC";
        font-size: 0.24rem;

        &:nth-of-type(3) {
          text-align: right;
        }
      }
    }

    .record-list-box {
      height: 55vh;
      overflow-y: auto;

      .record-list {
        .record {
          margin-top: 0.2rem;
          padding: 0.26rem 0.28rem;
          background: #121622;
          display: flex;
          align-items: center;
          justify-content: space-between;
          border-radius: 0.24rem;
          &:first-child {
            margin-top: 0;
          }

          .amount {
            flex: 1;
            color: #ffa800;
            font-size: 0.32rem;
          }

          .time {
            flex: 1;
            color: #bdbdbd;
            font-size: 0.22rem;
          }

          .status {
            flex: 1;
            color: #fff;
            font-size: 0.26rem;
            text-align: right;
            span {
              color: #4272ff;
            }
          }
        }
      }
    }
  }
}
</style>
